﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BotonJXC.BLL;
using BotonJXC.Entity;
using BotonJXC.Util;
using NHibernate;
using BotonJXC.DAO;

namespace BotonJXC.Forms
{
    public partial class InStockForm : BaseMdiForm
    {
        public static string[] type;
        private Int32 pageSize = 1;
        private Int32 totalSize;
        InStocksBll InStocksBll = null;
        InStocks InStocks = null;
        ProductBLL ProductBLL = null;
        WarehouseBLL WarehouseBLL = null;
        WarehouseProductBLL WarehouseProductBLL = null;
        OutStocksBLL OutStocksBLL = null;
        public InStockForm()
        {
           
            InitializeComponent();
        }

        private void InStockForm_Load(object sender, EventArgs e)
        {
            InitDepartment();
            InitWarehouse();
            InitStore();
            InitForm();
            LoadDate();
        }
        private void InitForm(){
            type = this.Tag.ToString().Split(',');
        }
        /// <summary>
        /// 初始化数据
        /// </summary>
        private void LoadDate() {

            InStocks SearchInStocks = new InStocks();
            SearchInStocks.InType = type[1];
            SearchInStocks.CreatePlace = type[0];

            if (InStocksBll == null) { InStocksBll = new InStocksBll(); }
            PageModel<InStocks> pageModel = InStocksBll.FindPage(SearchInStocks, (pageSize - 1) * 1, 1);
            //分页
            pageBtnControl(pageModel);

            if (pageModel.Results.Count == 0)
            {
                this.deletedBtn.Enabled = false;
                this.notAuditBtn.Enabled = false;
                this.auditBtn.Enabled = false;
                this.saveBtn.Enabled = false;
            }
            else
            {
 
                //赋值
                foreach (InStocks newInStocks in pageModel.Results)
                {
                    this.InStocks = newInStocks;
                }
                //已审核
                if (Utils.ToString((Int32)Status.AuditPass).Equals(this.InStocks.AuditStatus)
                    || Utils.ToString((Int32)Status.AuditNoPass).Equals(this.InStocks.AuditStatus))
                {
                    this.deletedBtn.Enabled = false;
                    this.auditBtn.Enabled = false;
                    this.saveBtn.Enabled = false;
                    this.notAuditBtn.Enabled = false;
                }
                else
                {
                    this.deletedBtn.Enabled = true;
                    this.auditBtn.Enabled = true;
                    this.saveBtn.Enabled = true;
                    this.notAuditBtn.Enabled = true;
                }

                //分页
                pageBtnControl(pageModel);

                //初始数据
                this.outSnText.Text = this.InStocks.InSn;
               // this.SalesmanLabel.Text = "业务员: " + this.InStocks.Salesman;
                this.MgDepartmentCboBox.Text = this.InStocks.MgDepartment == null ? "" : this.InStocks.MgDepartment.DepName;
                this.FinDepartmentCboBox.Text = this.InStocks.FinDepartment == null ? "" : this.InStocks.FinDepartment.DepName;
                this.InStoreCboBox.Text = this.InStocks.InStore == null ? "" : this.InStocks.InStore.StoreName;
                this.OutStoreCboBox1.Text = this.InStocks.OutStore == null ? "" : this.InStocks.OutStore.StoreName;
                this.EffDayText.Text = this.InStocks.EffDay == null ? "" : this.InStocks.EffDay.ToString();

                if (this.InStocks.OutStocks != null)
                {
                    this.OutStoreCboBox1.Enabled = false;
                    this.OutWarehouseCbobox1.Enabled = false;

                    this.OrdersSnLabel.Text = "相关单号: " + this.InStocks.OutStocks.OutSn;
                    this.OrdersTypeLabel.Text = "相关单据类型: " + StatusUtil.getOutStocksType(this.InStocks.OutStocks.OutType);
                }
                else {

                    this.OutStoreCboBox1.Enabled = true;
                    this.OutWarehouseCbobox1.Enabled = true;
                }
                this.OrdersStatusLabel.Text = "单据状态: " + StatusUtil.getAuditString(Int32.Parse(this.InStocks.AuditStatus == null ? "1" : this.InStocks.AuditStatus));
                if (this.InStocks.Account != null)
                {
              
                    this.AccountLabel2.Text = "制单人: " + this.InStocks.Account.Username;
     
                }
 
                this.CreateTimeLabel1.Text = "制单日期: " + string.Format("{0:yyyy/MM/dd HH:mm:ss}",  this.InStocks.CreateTime);
                if (this.InStocks.OutAuditAccount != null)
                    this.OutAuditAccountLabel.Text = "配送审核人: " + this.InStocks.OutAuditAccount.Username;
                this.OutAuditTimeLabel.Text = "配送审核日期: " + string.Format("{0:yyyy/MM/dd HH:mm:ss}", this.InStocks.OutAuditTime);
                if (this.InStocks.InAuditAccount != null)
                    this.InAuditAccountLabel.Text = "入库审核人:　" + this.InStocks.InAuditAccount.Username;
                this.InAuditTimeLabel.Text = "入库审核日期: " + string.Format("{0:yyyy/MM/dd HH:mm:ss}", this.InStocks.InAuditTime);
                this.PrintTimeLabel1.Text = "打印次数:　" + this.InStocks.PrintTime;
                this.RemarkText.Text = this.InStocks.Remark;

                this.OutWarehouseCbobox1.Text = this.InStocks.OutWarehouse == null ? "" : this.InStocks.OutWarehouse.Name;
    
               
 

                //初始化datagridView
                //清空dateGridView
                ProductsDataGridViewOrders.Rows.Clear();
                Double totalRealOutQuantity = 0;//实际配出数，报损数量
            
                //计划配送数量
                Double totalPlanQuantity = 0;
                //实际配入数量
                Double totalRealInQuantity = 0;
                foreach (InStocksProducts InStocksProducts in this.InStocks.InStocksProductsList)
                {
                    int index = this.ProductsDataGridViewOrders.Rows.Add();
                    this.ProductsDataGridViewOrders.Rows[index].Cells["ProductId"].Value = InStocksProducts.Product.ProductId;
                    this.ProductsDataGridViewOrders.Rows[index].Cells["ProductCode"].Value = InStocksProducts.Product.ProductCode;
                    this.ProductsDataGridViewOrders.Rows[index].Cells["Barcode"].Value = InStocksProducts.Product.Barcode;
                    this.ProductsDataGridViewOrders.Rows[index].Cells["ProductName"].Value = InStocksProducts.Product.ProductName;
                    this.ProductsDataGridViewOrders.Rows[index].Cells["Unit"].Value = InStocksProducts.Product.Unit == null ? "" : InStocksProducts.Product.Unit.UnitName;
                    this.ProductsDataGridViewOrders.Rows[index].Cells["PackSpec"].Value = InStocksProducts.Product.ProductSpec;

                    this.ProductsDataGridViewOrders.Rows[index].Cells["PlanQuantity"].Value = InStocksProducts.PlanQuantity; //计划配出数量
                    this.ProductsDataGridViewOrders.Rows[index].Cells["RealOutQuantity"].Value = InStocksProducts.RealOutQuantity;//实际配出数量
                    this.ProductsDataGridViewOrders.Rows[index].Cells["RealInQuantity"].Value = InStocksProducts.RealInQuantity;//实际配人数量
                    //this.ProductsDataGridViewOrders.Rows[index].Cells["DefPurPrice"].Value = InStocksProducts.Product.DefPurPrice;//默认进价
                   // this.ProductsDataGridViewOrders.Rows[index].Cells["RealOutQuantitys"].Value = InStocksProducts.RealOutQuantity;//报损数量

                   // ProductsDataGridViewOrders["inPurPrice", index].Value =
                   //       InStocksProducts.Product.PurPrice * (1 + InStocksProducts.Product.TaxRate);//含税进价 = 未税进价*(1+税率)
                   // ProductsDataGridViewOrders["DefPurPrice", index].Value = InStocksProducts.Product.DefPurPrice;//默认进价
                    //ProductsDataGridViewOrders["TaxRate", index].Value = InStocksProducts.Product.TaxRate;//税率
                    //计算总数
                    totalRealOutQuantity += InStocksProducts.RealOutQuantity;
                    totalPlanQuantity += InStocksProducts.PlanQuantity;
                    totalRealInQuantity += InStocksProducts.RealInQuantity;
                }
                //总数计算
                InsendNumTrueLabel.Text = "实际配人数量: " + totalRealInQuantity;//实际配人总数
                sendNumLabel.Text = "计划配出数量: " + totalPlanQuantity;
                OutsendNumTrueLabel.Text = "实际配出数量: " + totalRealOutQuantity;

            }
        }
        /// <summary>
        /// 分页库存
        /// </summary>
        /// <param name="pageModel"></param>
        private void pageBtnControl(PageModel<Entity.InStocks> pageModel)
        {
            this.totalSize = pageModel.PageCount;

            if (pageSize == totalSize)
            {
                this.next.Enabled = false;
            }
            else
            {
                this.next.Enabled = true;
            }
            if (pageSize == 1)
            {
                this.up.Enabled = false;
            }
            else
            {
                this.up.Enabled = true;
            }
        }


        /// <summary>
        /// 表单还原
        /// </summary>
        private void returnInit()
        {
            this.outSnText.Text = "";
            this.SalesmanLabel.Text = "业务员: ";
            this.EffDayText.Text = "";
            this.OrdersSnLabel.Text = "相关单号: ";
            this.OrdersTypeLabel.Text = "相关单据类型: ";
            this.OrdersStatusLabel.Text = "单据状态: ";
            this.AccountLabel2.Text = "制单人: "; 
            this.OutAuditAccountLabel.Text = "配送审核人: ";
            this.OutAuditTimeLabel.Text = "配送审核日期: ";
            this.InAuditAccountLabel.Text = "入库审核人:　";
            this.InAuditTimeLabel.Text = "入库审核日期: ";
            this.PrintTimeLabel1.Text = "打印次数:　";
            this.RemarkText.Text = "";          
            InsendNumTrueLabel.Text = "实际配人数量: ";
            sendNumLabel.Text = "计划配出数量: ";
            OutsendNumTrueLabel.Text = "实际配出数量: ";
            this.OutWarehouseCbobox1.SelectedIndex = 0;//选中第一项
            this.InWarehouseCbobox.SelectedIndex = 0;//选中第一项
            this.MgDepartmentCboBox.SelectedIndex = 0;//选中第一项
            this.FinDepartmentCboBox.SelectedIndex = 0;//选中第一项
            this.InStoreCboBox.SelectedIndex = 0;//选中第一项
            this.OutStoreCboBox1.SelectedIndex = 0;//选中第一项
        }
        /// <summary>
        /// 点新建表单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void addBtn_Click(object sender, EventArgs e)
        {
            this.saveBtn.Tag = "add";
            this.deletedBtn.Enabled = false;
            this.saveBtn.Enabled = true;
            ProductsDataGridViewOrders.Rows.Clear();
            returnInit();
        }



        private void InitStore()
        {
            StoreBLL StoreBLL = new StoreBLL();
 
            OutStoreCboBox1.DataSource = StoreBLL.FindAll();
            InStoreCboBox.DataSource = StoreBLL.FindAll();

 
            OutStoreCboBox1.DisplayMember = "StoreName";//显示字段
            OutStoreCboBox1.ValueMember = "StoreId";//id值
            InStoreCboBox.DisplayMember = "StoreName";//显示字段
            InStoreCboBox.ValueMember = "StoreId";//id值
        }
        /// <summary>
        /// 初始化库存地
        /// </summary>
        private void InitWarehouse()
        {
            if(WarehouseBLL==null){
                WarehouseBLL = new WarehouseBLL();
            }
              
            InWarehouseCbobox.DataSource = WarehouseBLL.FindByUserAll();
            OutWarehouseCbobox1.DataSource = WarehouseBLL.FindByUserAll();
 
            InWarehouseCbobox.DisplayMember = "Name";//显示字段
            InWarehouseCbobox.ValueMember = "WarehouseId";//id值

            OutWarehouseCbobox1.DisplayMember = "Name";//显示字段
            OutWarehouseCbobox1.ValueMember = "WarehouseId";//id值


        }

        /// <summary>
        /// 初始化财务部部门 和管理部
        /// </summary>
        private void InitDepartment()
        {
            DepartmentBLL DepartmentBLL = new DepartmentBLL();
            IList<Department> DepartmentList = DepartmentBLL.FindAll();

            this.FinDepartmentCboBox.DataSource = DepartmentList;
            this.MgDepartmentCboBox.DataSource = DepartmentList;

            MgDepartmentCboBox.DisplayMember = "DepName";//显示字段
            MgDepartmentCboBox.ValueMember = "DepId";//id值

            FinDepartmentCboBox.DisplayMember = "DepName";//显示字段
            FinDepartmentCboBox.ValueMember = "DepId";//id值

        }

        private void next_Click(object sender, EventArgs e)
        {
            pageSize += 1;
            LoadDate();
        }

        private void up_Click(object sender, EventArgs e)
        {
            pageSize -= 1;
            LoadDate();
        }

        private void deletedBtn_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认要删除当前入库单吗？", "删除确认",
       MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)

                if (InStocksBll == null) InStocksBll = new InStocksBll();
            bool result = InStocksBll.DeleteInStocks(InStocks);
            if (result)
            {
                MessageBox.Show("单据删除成功!");
                LoadDate();
            }
            else
            {
                MessageBox.Show("单据删除失败!");
            }

        }
        /// <summary>
        /// 单据审核
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void auditBtn_Click(object sender, EventArgs e)
        {
            InStocks.AuditStatus = Utils.ToString((Int32)Status.AuditPass);
            auditMethod();
        }
        /// <summary>
        /// 拒审
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void notAuditBtn_Click(object sender, EventArgs e)
        {
            InStocks.AuditStatus = Utils.ToString((Int32)Status.AuditNoPass);
            auditMethod();
        }
        private void auditMethod()
        {
            InStocks.AuditAccount = LoginForm.Account;
            InStocks.AuditTime = DateTime.Now;

            InStocks.InAuditAccount = LoginForm.Account;
            InStocks.InAuditTime = DateTime.Now;
            bool result = false;
            if (InStocks.OutStocks != null && Utils.ToString((Int32)Status.AuditPass).Equals(InStocks.AuditStatus))
            {
                OutStocks OutStocks = InStocks.OutStocks;
                OutStocks.InAuditAccount = LoginForm.Account;
                OutStocks.InAuditTime = DateTime.Now;

                Dictionary<int, Double> dic = new Dictionary<int, Double>();

                foreach (InStocksProducts InStocksProducts in InStocks.InStocksProductsList)
                {
                    dic.Add(InStocksProducts.Product.ProductId, InStocksProducts.RealInQuantity);
                }

                foreach (OutStocksProducts OutStocksProducts in OutStocks.ProductsList)
                {
                    OutStocksProducts.RealInQuantity = dic[OutStocksProducts.Product.ProductId];
                }

                result = Modify(InStocks, OutStocks,true);
            }
            else
            {
                result = Modify(InStocks);
            }
            if (result)
            {

                MessageBox.Show("单据审核成功!");
                this.LoadDate();
            }
            else
            {

                MessageBox.Show("单据审核失败!");
            }
        }

        private void saveBtn_Click(object sender, EventArgs e)
        {
            if (this.InStocksBll == null) this.InStocksBll = new InStocksBll();

            if ("add".Equals(this.saveBtn.Tag))
            {

                bool result = InStocksBll.SaveInStocks(getInStocks());
                if (result)
                {
                    MessageBox.Show("单据添加成功!");
                    LoadDate();
                    this.saveBtn.Tag = "up";
                    this.saveBtn.Enabled = true;
                }
                else
                {
                    MessageBox.Show("单据添加失败!");
                }
            }
            else if ("up".Equals(this.saveBtn.Tag))
            {
                bool result =  Modify(getInStocks());
                if (result)
                {
                    MessageBox.Show("单据修改成功!");
                }
                else
                {
                    MessageBox.Show("单据修改失败!");
                }
            }
        }

        private Entity.InStocks getInStocks()
        {
            InStocks InStocks = null;

            if ("add".Equals(this.saveBtn.Tag))
            {
                InStocks = new InStocks();
                if ("1".Equals(type[0]))
                {
                    InStocks.InSn = new BillSNGenerator().GeneratorBillSn(BillType.InStock, BillSource.Store, LoginForm.StoreId);
                }
                else if ("2".Equals(type[0]))
                {
                    InStocks.InSn = new BillSNGenerator().GeneratorBillSn(BillType.InStock, BillSource.DistributionCenter, LoginForm.StoreId);
                }
                else if ("4".Equals(type[0]))
                {
                    InStocks.InSn = new BillSNGenerator().GeneratorBillSn(BillType.InStock, BillSource.Process, LoginForm.StoreId);
                }
            
                //出库地
                //OutStocks.OutWarehouse = Orders.Warehouse;
                InStocks.Account = LoginForm.Account;
                InStocks.CreateTime = DateTime.Now;

                InStocks.OrgDepId = LoginForm.DepId;
                InStocks.OrgId = LoginForm.StoreId;
                InStocks.AuditStatus = "1";

                InStocks.CreatePlace = type[0];
                InStocks.InType = type[1];
                //业务员
              InStocks.Salesman = "";
            }
            else
            {
                InStocks = this.InStocks;
                //业务员
                InStocks.Salesman = LoginForm.Account.Username;
            }
             

                //配出分店
            InStocks.OutStore = (Store)this.OutStoreCboBox1.SelectedItem;//获取当前选项 this.OutStoreCboBox1
                //配出库存地
            InStocks.OutWarehouse = (Warehouse)this.OutWarehouseCbobox1.SelectedItem;
         
             
            //配入分店
              InStocks.InStore = (Store)this.InStoreCboBox.SelectedItem;//获取当前选项 this.OutStoreCboBox1
            //配入库存地
              InStocks.InWarehouse = (Warehouse)this.InWarehouseCbobox.SelectedItem;


            //备注
              InStocks.Remark = this.RemarkText.Text;
            //管理部
              InStocks.MgDepartment = (Department)this.MgDepartmentCboBox.SelectedItem;
            //财务部
              InStocks.FinDepartment = (Department)this.FinDepartmentCboBox.SelectedItem;
 
           
            //有效期
              InStocks.EffDay = Utils.ToInt(this.EffDayText.Text, 0);
            //货品信息           
              InStocks.InStocksProductsList = getInStocksProducts();

              return InStocks;
        }

        /// <summary>
        /// 获取货品信息
        /// </summary>
        /// <returns></returns>
        public IList<InStocksProducts> getInStocksProducts()
        {
            IList<InStocksProducts> oplist = new List<InStocksProducts>();
            for (int i = 0; i < ProductsDataGridViewOrders.Rows.Count - 1; i++)//如果DataGridView中有空的数据，则提示数据输入不完整并退出添加，不包括标题行
            {
                InStocksProducts InStocksProducts = new InStocksProducts();
                Product Product = new Product();
                Product.ProductId = Utils.ToInt(ProductsDataGridViewOrders["ProductId", i].Value, 0);
                InStocksProducts.Product = Product;

                InStocksProducts.PlanQuantity = Utils.ToDouble(ProductsDataGridViewOrders["PlanQuantity", i].Value, 0);

                InStocksProducts.RealOutQuantity = Utils.ToDouble(ProductsDataGridViewOrders["RealOutQuantity", i].Value, 0);


                InStocksProducts.RealInQuantity = Utils.ToDouble(ProductsDataGridViewOrders["RealInQuantity", i].Value, 0);
                InStocksProducts.Price = Utils.ToDouble(ProductsDataGridViewOrders["Price", i].Value, 0);
                InStocksProducts.Amount = InStocksProducts.Price * InStocksProducts.RealOutQuantity;
           //     InStocksProducts.TaxRate = Utils.ToDouble(ProductsDataGridViewOrders["TaxRate", i].Value, 0);
                //OutStocksProducts.AllocateAddRate = Utils.ToDouble(ProductsDataGridViewOrders["AllocateAddRate", i].Value, 0);

                InStocksProducts.InputSeq = i + 1;

                oplist.Add(InStocksProducts);
            }
            return oplist;
        }

        private void ProductsDataGridViewOrders_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {

            string parameter = ProductsDataGridViewOrders.Columns[e.ColumnIndex].Name;
            if ("ProductCode".Equals(parameter) || "Barcode".Equals(parameter) || "ProductName".Equals(parameter))
            {
                string value = ProductsDataGridViewOrders.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null ? "" : ProductsDataGridViewOrders.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                if (!"".Equals(value))
                {
                    if (ProductBLL == null) ProductBLL = new ProductBLL();
                    Product Product = ProductBLL.FindProduct(parameter, value);
                    if (Product == null)
                    {
                        MessageBox.Show("没有对应的货品");
                        ProductsDataGridViewOrders.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "";
                    }
                    else
                    {
                        ProductsDataGridViewOrders["ProductId", e.RowIndex].Value = Product.ProductId;
                        ProductsDataGridViewOrders["ProductCode", e.RowIndex].Value = Product.ProductCode;
                        ProductsDataGridViewOrders["Barcode", e.RowIndex].Value = Product.Barcode;
                        ProductsDataGridViewOrders["ProductName", e.RowIndex].Value = Product.ProductName;
                        ProductsDataGridViewOrders["Unit", e.RowIndex].Value = Product.Unit == null ? "" : Product.Unit.UnitName;
                        ProductsDataGridViewOrders["PackSpec", e.RowIndex].Value = Product.ProductSpec; 
                    }
                }


            }
            if ("RealOutQuantity".Equals(parameter) || "InMoney".Equals(parameter) || "RealInQuantity".Equals(parameter) || "PlanQuantity".Equals(parameter))
            {
                Int32 RealOutQuantity = Utils.ToInt(ProductsDataGridViewOrders["RealOutQuantity", e.RowIndex].Value, 0);
                Int32 RealInQuantity = Utils.ToInt(ProductsDataGridViewOrders["RealInQuantity", e.RowIndex].Value, 0);
                Int32 InMoney = Utils.ToInt(ProductsDataGridViewOrders["InMoney", e.RowIndex].Value, 0);
                if (RealOutQuantity != 0 && InMoney != 0)
                {
                    ProductsDataGridViewOrders["totalInMoney", e.RowIndex].Value = RealOutQuantity * InMoney;
                }
                if (RealOutQuantity != 0)
                {
                    //件数
                    ProductsDataGridViewOrders["RealOutQuantityPack", e.RowIndex].Value = RealOutQuantity;
                }
                if (RealOutQuantity != 0)
                {
                    ProductsDataGridViewOrders["RealInQuantityPack", e.RowIndex].Value = RealInQuantity;
                }
                totalNumber();
            }
        }

        /// <summary>
        /// 统计总算
        /// </summary>
        private void totalNumber()
        {
            Double totalRealOutQuantity = 0;//实际配出数，报损数量
            //计划配送数量
            Double totalPlanQuantity = 0;
            //实际配入数量
            Double totalRealInQuantity = 0;
            for (int i = 0; i < ProductsDataGridViewOrders.Rows.Count - 1; i++)//如果DataGridView中有空的数据，则提示数据输入不完整并退出添加，不包括标题行
            {
                //计算总数
                totalPlanQuantity += Utils.ToDouble(this.ProductsDataGridViewOrders["PlanQuantity", i].Value, 0);
                totalRealOutQuantity += Utils.ToDouble(this.ProductsDataGridViewOrders["RealOutQuantity", i].Value, 0);
                totalRealInQuantity += Utils.ToDouble(this.ProductsDataGridViewOrders["RealInQuantity", i].Value, 0);
            }
            //总数计算
 
            InsendNumTrueLabel.Text = "实际配人数量: " + totalRealInQuantity;//实际配人总数
            sendNumLabel.Text = "计划配出数量: " + totalPlanQuantity;
            OutsendNumTrueLabel.Text = "实际配出数量: " + totalRealOutQuantity;

        }

        private void InStoreCboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            Store Store = (Store)this.InStoreCboBox.SelectedItem;//获取当前选项
            if (WarehouseBLL == null)
                WarehouseBLL = new WarehouseBLL();
            InWarehouseCbobox.DataSource = WarehouseBLL.FindWarehouseByStoreId(Store.StoreId, true);
        }

        private void OutStoreCboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Store Store = (Store)this.OutStoreCboBox1.SelectedItem;//获取当前选项
            if (WarehouseBLL == null)
                WarehouseBLL = new WarehouseBLL();
            OutWarehouseCbobox1.DataSource = WarehouseBLL.FindWarehouseByStoreId(Store.StoreId, true);
        }

        private bool Modify(InStocks InStocks)
        {
            return Modify(InStocks ,null,false);
        }
        private bool Modify(InStocks InStocks, OutStocks OutStocks ,  bool OptQuantity)
        {


            ITransaction trans = HBHelper.BeginTransaction();
            try
            {

                InStocksBll.Update(InStocks);
                if (OutStocks != null) {
                    if (OutStocksBLL == null) OutStocksBLL = new OutStocksBLL();
                    OutStocksBLL.Update(OutStocks);
                }
                if (OptQuantity)
                {
                    updateWarehOuseProduct(InStocks);
                }
                trans.Commit();
                return true;
            }
            catch (HibernateException ex)
            {
                trans.Rollback();
             
                return false;
            }
            finally
            {
                HBHelper.Close();
            }

       

        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            this.LoadDate();
        }

              /// <summary>
        /// 更新库存地货品信息
        /// </summary>
        /// <param name="OutStocks"></param>
        public void updateWarehOuseProduct(InStocks InStocks)
        {
            if (WarehouseProductBLL == null) { WarehouseProductBLL = new WarehouseProductBLL(); }
            foreach (InStocksProducts InStocksProducts in InStocks.InStocksProductsList)
            {
                WarehouseProductBLL.OptQuantity(InStocks.OutWarehouse.WarehouseId, InStocksProducts.Product.ProductId, InStocksProducts.RealInQuantity);
            }
        }
    }
}
